package androidcamp.socialfitnesstracker.util;

import java.util.ArrayList;

public class TimeInterval {
    
    private long timeInSeconds;
    private int hours;
    private int minutes;
    private int seconds;
    
    public static final int SECONDS_IN_MINUTE = 60;
    public static final int SECONDS_IN_HOUR = SECONDS_IN_MINUTE * 60;
    
    public TimeInterval(long timeInSeconds) {
        this.timeInSeconds = timeInSeconds;
        this.hours = (int)(timeInSeconds / SECONDS_IN_HOUR);
        timeInSeconds %= SECONDS_IN_HOUR;
        this.minutes = (int)(timeInSeconds / SECONDS_IN_MINUTE);
        timeInSeconds %= SECONDS_IN_MINUTE;
        this.seconds = (int)timeInSeconds;
    }

    public long getTimeInSeconds() {
        return timeInSeconds;
    }

    public int getHours() {
        return hours;
    }

    public int getMinutes() {
        return minutes;
    }

    public int getSeconds() {
        return seconds;
    }
    
    public String getHumanFormat() {
        ArrayList<String> segments = new ArrayList<String>();
        if(getHours() > 0)
            segments.add(String.format(getHours() == 1 ? "%d hour" : "%d hours", getHours()));
        if(getMinutes() > 0)
            segments.add(String.format(getMinutes() == 1 ? "%d minute" : "%d minutes", getMinutes()));
        if(getSeconds() > 0)
            segments.add(String.format(getSeconds() == 1 ? "%d second" : "%d seconds", getSeconds()));
        StringBuffer buf = new StringBuffer();
        for(int i=0; i < segments.size(); i++) {
            buf.append(segments.get(i));
            if(i < segments.size()-1)
                buf.append(", ");
        }
        return buf.toString();
    }

}
